<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseItemProductBase extends ProductBase {
	var $table = '';
	var $prefix = '';
	
	//
	var $table_product = 'pp_products';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $format = false;
	var $format_check = false;
	var $format_prefix = 'field_';
	var $item_link = false;
	var $item_link_owner = false;
	var $item_fields = array();
	var $list_fields = array();
	var $search_fields = array('name');
	var $search_type = 'or';
	var $sort_order = true;
	var $sort_fields = array('sort_order+', 'name+');
	var $filter_fields = array();
	var $filter_map = array();
	var $filter_query = array();
	
	//
	var $filters = array();
	var $triggers = array();
	
	//constructor
	function BaseItemProductBase() {
		parent::ProductBase();
	}
	
	function setProperty($name, $value = '') {
		if( is_array($name) ) {
			foreach($name as $k => $v) {
				$this->$k = $v;
			}
		} else {
			$this->$name = $value;
		}
	}
	
	function getProperty($name) {
		if( isset($this->$name) ) {
			return $this->$name;
		}
		
		return NULL;
	}
	
	function property($data) {
		if( is_array($data) ) {
			return $this->setProperty($data);
		} else {
			return $this->getProperty($data);
		}
	}
	
	function _pk() {
		return $this->prefix . $this->pkey;
	}
	
	function _pf($field) {
		return $this->prefix . $field;	
	}
	
	function setFilterQuery($name, $value = '') {
		if( is_array($name) ) {
			$this->filter_query = $name;
		} else {
			$this->filter_query[$name] = $value;
		}
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		$query = "select a.* from " . $this->table . " a "
				. " where " . $this->_query_owner('a.products_id') . " " 
				. $query_str . $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		$query = "select count(a.*) as count from " . $this->table . " a " 
				. " where " . $this->_query_owner('a.products_id') . " " 
				. $query_str;
		
		return $this->adb->getCount($query);
	}
	
	function getItemsDetail($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		$query = "select a.* from " . $this->table . " a,  " . $this->table_product . " p "
				. " where " . $this->_query_owner('a.products_id') . " and a.products_id = p.products_id " 
				. $query_str . $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getItemsDetailTotal($filter = '', $check = 0) {
		$query_str = "";
		$query_str = $this->_get_query_str($filter, $check);
		
		$query = "select count(a.*) as count from " . $this->table . " a, " . $this->table_product . " p " 
				. " where " . $this->_query_owner('a.products_id') . " and a.products_id = p.products_id " 
				. $query_str;
		
		return $this->adb->getCount($query);
	}
	
	//
	function checkItem($items_ids, $direction = 0) {
		$query_str = "";
		$query_str .= " and " . $this->_query_owner('products_id') . " and " . $this->prefix . "id in (" . $this->_implode(', ', $items_ids) . ")";
		$count = $this->adb->getCountA($this->table, "1 " . $query_str);
		if( $count == count($items_ids) ) {
			return true;
		}
		
		return false;
	}
	
	function addItem($data_array) {
		if( isset($data_array[$this->prefix . 'id']) ) {
			return false;
		}
		$data_array = func_db_input($data_array);
		$data_array['products_id'] = $this->owner;
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		return $this->_insert($this->table, $data_array);
	}
	
	function addItemB($data_array) {
		if( !isset($data_array[$this->prefix . 'id']) ) {
			return false;
		}
		$params = "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$data_array[$this->prefix . 'id'] . "'";
		$count = $this->adb->getCountA($this->table, $params);
		if( $count == 0 ) {
			return $this->addItem($data_array);
		} else {
			return 0;
		}
	}
	
	function getItem($items_id) {
		$query = "select * from " . $this->table . " where " . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'";
		$row = $this->adb->getRow($query);
		
		return $row;
	}
	
	function updateItem($items_id, $data_array) {
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		return $this->_update($this->table, $data_array, "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function updateItems($items_ids, $data_array) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->_update($this->table, $data_array, "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->_update($this->table, $data_array, "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id in (" . $query_ids . ")");
			}
		}
	}
	
	function deleteItem($items_id) {
		$query = "delete from " . $this->table . " where " . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'";
		return $this->adb->query($query);
	}
	
	function deleteItems($items_ids) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->adb->deleteA($this->table, "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->adb->deleteA($this->table, "" . $this->_query_owner('products_id') . " and " . $this->prefix . "id in (" . $query_ids . ")");
			}
		}
	}
	
	function setItemsStatus($items_ids, $status = 1) {
		if( is_array($items_ids) ) {
			$this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
		}
	}
	
	//
	function check($items_id) {
		$count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
		
		return ($count > 0) ? true : false;
	}
	
	function get($items_id) {
		return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function add($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		return $this->adb->insertA($this->table, $data_array);
	}
	
	function update($items_id, $data_array) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function delete($items_id) {
		return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	//
	function bindItemsProducts($items) {
		$products_ids = $this->getListKey($items, 'products_id');
		if( is_array($products_ids) ) {
			global $pObj;
			$products = $pObj->getProducts(array('ids' => $products_ids), -1, false);
			$products = $this->setListKey($products, 'products_id');
			for($i=0, $n=sizeof($items); $i<$n; $i++) {
				$items[$i] = array_merge($items[$i], $products[$items[$i]['products_id']]);
			}
		}
		
		return $items;
	}
	
	//
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and a." . $this->prefix . "name like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['pids']) ) {
				$query_str .= " and a.products_id in (" . $this->_implode(', ', $filter['pids']) . ")";
			}
			if( isset($filter['ids']) ) {
				$query_str .= " and a." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ")";
			}
			if( isset($filter['start']) && $this->_not_null($filter['start']) == true ) {
				$query_str .= " and a." . $this->prefix . "date_added >= '" . $filter['start'] . "' ";
			}
			if( isset($filter['end']) && $this->_not_null($filter['end']) == true ) {
				$query_str .= " and a." . $this->prefix . "date_added <= '" . $filter['end'] . "' ";
			}
		}
		//
		$query_str .= $this->_get_query_str_custom($filter, $check);
		
		return $query_str;
	}
	
	function _get_query_str_custom($filter = '', $check = 0) {
		return "";
	}
}
//
?>